<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

/**
 * 订单中间表模型
 * Class OrdersGoods
 * @package App\Models\CacheModel
 */
class OrdersGoods extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_orders_goods';
    protected $cacheModel = 'orders.orders_goods';

    /**
     * 订单中间表添加
     */
    public function SaveData($map)
    {
        try
        {
            $this->saveAll($map);
        }
        catch (\Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 批量获取信息
     * @param $ids
     * @param array $filed
     * @return array|false
     * @throws \Exception
     */
    public function batchGetInfo($ids, array $filed = [])
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->batchGetCache($ids, function ($ids)
            {
                $ids = is_array($ids) ? $ids : explode(',', $ids);
                $where = ['oid' => ['in', $ids]];
                $field = 'oid,gid,rid';
                try
                {
                    $data = $this
                        ->field($field)
                        ->where($where)
                        ->selectOrFail();
                    $data = json_decode(json_encode($data, true), true);
                    foreach ($data as $datum)
                    {
                        $arr[$datum['oid']][] = json_encode($datum, true);
                    }
                }
                catch (\Exception $e)
                {
                    $arr = [];
                }
                return $arr;
            }, $filed, 'list_details');

        return $data;
    }

    /**
     * 数据库批量获取校准生辰数据
     * @param $ids
     * @param array $type [0=>uid,1=>type]
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType = 'DESC')
    {
        $ids = is_array($ids) ? $ids : explode(',', $ids);
        $where = ['oid' => ['in', $ids]];
        $field = 'oid,gid,rid';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->selectOrFail();
            $data = json_decode(json_encode($data, true), true);
            foreach ($data as $datum)
            {
                $arr[$datum['oid']][] = json_encode($datum, true);
            }
        }
        catch (\Exception $e)
        {
            $arr = [];
        }
        return $arr;
    }
}
